﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace HoangPhap.Utilities
{
    public class Spider
    {
        public static Spider Instance
        {
            get { return new Spider(); }
        }

        public string GetWebContent(string s)
        {
            try
            {
                var objWebRequest = (HttpWebRequest) WebRequest.Create(s);
                objWebRequest.Timeout = 200000;
                objWebRequest.MaximumAutomaticRedirections = 4;
                objWebRequest.MaximumResponseHeadersLength = 4;
                objWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
                if (!(Configs.ProxyServer.Length < 1) || !(Configs.ProxyPort.Length < 1))
                {
                    objWebRequest.Proxy = new WebProxy(Configs.ProxyServer, int.Parse(Configs.ProxyPort))
                                              {
                                                  Credentials =
                                                      new NetworkCredential(Configs.ProxyUserName, Configs.ProxyPassword,
                                                                            Configs.ProxyUserDomain)
                                              };
                }
                objWebRequest.Credentials = CredentialCache.DefaultCredentials;

                var objWebResponse = (HttpWebResponse) objWebRequest.GetResponse();
                var receiceStream = objWebResponse.GetResponseStream();
                if (receiceStream != null)
                {
                    var readStream = new StreamReader(receiceStream, Encoding.UTF8);
                    var strContent = readStream.ReadToEnd();

                    objWebResponse.Close();
                    readStream.Close();

                    return Regex.Replace(strContent, "\\t|\\r|\\n", "");
                }
                return "False";
            }
            catch (Exception)
            {
                return "False";
            }
        }

        public Stream GetStreamCntWeb(string s)
        {
            try
            {
                var objWebRequest = (HttpWebRequest) WebRequest.Create(s);
                objWebRequest.Timeout = 200000;
                objWebRequest.MaximumAutomaticRedirections = 4;
                objWebRequest.MaximumResponseHeadersLength = 4;
                objWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
                if (!(Configs.ProxyServer.Length < 1) || !(Configs.ProxyPort.Length < 1))
                {
                    objWebRequest.Proxy = new WebProxy(Configs.ProxyServer, int.Parse(Configs.ProxyPort))
                                              {
                                                  Credentials =
                                                      new NetworkCredential(Configs.ProxyUserName, Configs.ProxyPassword,
                                                                            Configs.ProxyUserDomain)
                                              };
                }
                objWebRequest.Credentials = CredentialCache.DefaultCredentials;

                return (objWebRequest.GetResponse()).GetResponseStream();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}